//
//  HomeViewController.m
//  SheShiDu
//
//  Created by 单剑秋 on 16/6/11.
//  Copyright © 2016年 单剑秋. All rights reserved.
//

#import "HomeViewController.h"
#import "CycleTableViewCell.h"
#import "SDCycleScrollView.h"
#import "ButtonTableViewCell.h"
#import "PackageTableViewCell.h"
#import "VerticalTableViewCell.h"
#import "BanderViewController.h"
#import "AdvantageViewController.h"

@interface HomeViewController ()<UITableViewDelegate, UITableViewDataSource, SDCycleScrollViewDelegate>

@property (nonatomic, strong) UITableView *wholeTableView;
@property (nonatomic, strong) NSMutableArray *picArr;

@end

@implementation HomeViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.picArr = [NSMutableArray array];
  
    for (int i = 0; i < 4; i++) {
        NSString *picStr = [NSString stringWithFormat:@"tu%d",i];
        UIImage *totol = [UIImage imageNamed:picStr];
        [_picArr addObject:totol];
//        NSLog(@"%@", picStr);
    }
    
    [self addTableView];
    
}

- (void)addTableView
{
    
    self.wholeTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _wholeTableView.delegate = self;
    _wholeTableView.dataSource = self;
    _wholeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _wholeTableView.allowsSelection = NO;
    [self.view addSubview:_wholeTableView];
    
    self.wholeTableView.bounces = NO;
    
    [_wholeTableView registerClass:[CycleTableViewCell class] forCellReuseIdentifier:@"CycleCell"];
    [_wholeTableView registerClass:[ButtonTableViewCell class] forCellReuseIdentifier:@"ButtonCell"];
    [_wholeTableView registerClass:[VerticalTableViewCell class] forCellReuseIdentifier:@"Vertical"];
    [_wholeTableView registerClass:[PackageTableViewCell class] forCellReuseIdentifier:@"test"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 2) {
        return 1;
    } else if (section == 3) {
        return 0;
    } else {
        return 2;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            CycleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CycleCell" forIndexPath:indexPath];
            cell.cycleView.delegate = self;
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                cell.cycleView.localizationImageNamesGroup = _picArr;
//            });
            return cell;
        } else {
            ButtonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ButtonCell" forIndexPath:indexPath];
            [cell.materialButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
            return cell;
        }
    } else if (indexPath.section == 1) {
        VerticalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Vertical" forIndexPath:indexPath];
        if (indexPath.row == 0) {
            [cell.mainButton setImage:[UIImage imageNamed:@"new"] forState:UIControlStateNormal];
        } else {
            [cell.mainButton setImage:[UIImage imageNamed:@"old"] forState:UIControlStateNormal];
        }
        return cell;
    } else {
        PackageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"test"];
        return cell;
    }
 
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 4)];
    backView.backgroundColor = [UIColor colorWithRed:237 / 255.0 green:231 / 255.0 blue:227 / 255.0 alpha:1];
    return backView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat Height = 0;
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            Height = self.view.frame.size.height / 3.2;
           
        } else if (indexPath.row == 1) {
            Height = self.view.frame.size.height / 9.3;
        }
    } else if (indexPath.section == 1) {
        Height = self.view.frame.size.height / 6.1;
    } else {
        Height = self.view.frame.size.height / 5.75;
    }
    return Height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    } else {
        return 3;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 点击方法
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    BanderViewController *banderVC = [[BanderViewController alloc]init];
    [banderVC.navigationController setNavigationBarHidden:YES animated:YES];
    [banderVC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:banderVC animated:YES];
}

- (void)buttonAction {
    AdvantageViewController *advantageVC = [[AdvantageViewController alloc]init];
    [self.navigationController pushViewController:advantageVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
